using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Timers;

public class KillerCreater : MonoBehaviour
{
    // Start is called before the first frame update
    public GameObject killerPrefab;
    public int interval_time = 1000;
    private Timer t;
    private System.Random rd;
    private bool isTime = false;
    
    void Start()
    {
        int interval = 0;
        t = new Timer();
        rd = new System.Random((int)System.DateTime.Now.Ticks);
        t.Stop();
        t.Elapsed += TimerCallBackHook;
        interval = rd.Next() % interval_time;
        t.Interval = interval;
        t.AutoReset = true;
        t.Start();
    }

    // Update is called once per frame
    void Update()
    {
        if(isTime)
        {
            Instantiate(this.killerPrefab);
            isTime = false;
        }
    }

    void TimerCallBackHook(object source, System.Timers.ElapsedEventArgs e)
    {
        int interval;
        t.Stop();
        if(isTime == false)
        {
            interval = rd.Next() % 1000;
            t.Interval = interval;
            isTime = true;
        }
        t.Start();
    }
}
